using UnityEngine;
using UnityEngine.UI;

public class UIB_TextLegacy_Script : MonoBehaviour
{
    [Header("字体颜色渲染间隔")] 
    public float interval = 1;
    
    private Text text;
    private string originalText;
    private float timer;
    private int counter;
    private string headTag = "<color=lime>";
    private string tailTag = "</color>";
    void Awake()
    {
        text = GetComponent<Text>();
        originalText = text.text;
    }

    // 每帧调用两次
    void OnGUI()
    {
        timer += Time.deltaTime;
        // 渲染
        if (timer > interval)
        {
            if (counter < originalText.Length)
            {
                counter++;
                text.text = headTag + originalText.Insert(counter, tailTag);
            }
            else
            {
                text.text = headTag + originalText + tailTag;
            }
            // 如果不是空格，计时器归0
            if (counter < originalText.Length && originalText[counter] != ' ')
            {
                // 计时器归0
                timer = 0;
            }
        }
    }
}